package leetcode.sort;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: feiwang_6
 * @create: 2020/7/19 11:55
 * @description: 冒泡排序 [时间复杂度O(n^2)]
 */
public class BubbleSort {
    
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        try {
            System.out.println(1/0);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("11111111111111111");
        }
    }
    
    @Test
    public void test(){
        Integer[] numbers = new Integer[]{3, 16, 89, -1, -3, 10, 45, 0};
        bubbleSort(numbers);
        System.out.println(Arrays.asList(numbers));
        System.out.println(getEnumIndex(100, Arrays.asList(numbers), 0, numbers.length-1));
    }
    
    public void bubbleSort(Integer[] numbers){
        for(int i = 0; i < numbers.length; i++){
            for(int j = 0; j < numbers.length-1-i; j++){
                if(numbers[j] > numbers[j+1]){
                    int temp = numbers[j];
                    numbers[j] = numbers[j+1];
                    numbers[j+1] = temp;
                }
            }
        }
    }

    /**
     * 二分查找
     * @param val
     * @param list
     * @param min
     * @param max
     * @return
     */
    public int getEnumIndex(int val, List<Integer> list, int min, int max){
        if(min > max){
            return -1;
        }
        int mid = (min + max)/2;
        if(list.get(mid) == val){
            return mid;
        }
        if(list.get(mid) > val){
            return getEnumIndex(val, list, min, mid-1);
        }
        if(list.get(mid) < val){
            return getEnumIndex(val, list, mid+1, max);
        }
        return -1;
    }
}
